﻿using AutoMapper;
using Domain.User;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Account;
using MES.Server.API.Dto.Account;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Account
{
    public class SupplierGetHandler:IRequestHandler<SupplierGetCommand,ApiPaging<OtherRoleDTO>>
    {
        private readonly IRepository<AccountModel> accountway;
        private readonly IRepository<AccountRoleModel> accountroleway;
        private readonly IRepository<RoleModel> roleway;
        private readonly ILogger<LoginHandler> log;
        private readonly IMapper mapper;
        private readonly IConfiguration configuration;

        public SupplierGetHandler(IRepository<AccountModel> accountway, IRepository<AccountRoleModel>
            accountroleway, IRepository<RoleModel> roleway, ILogger<LoginHandler> log, IMapper mapper, IConfiguration configuration)
        {
            this.accountway = accountway;
            this.accountroleway = accountroleway;
            this.roleway = roleway;
            this.log = log;
            this.mapper = mapper;
            this.configuration = configuration;
        }
        /// <summary>
        /// 获取用供应商列表
        /// </summary>
        /// <param name="request">用户外键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>用户列表</returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<OtherRoleDTO>> Handle(SupplierGetCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<OtherRoleDTO> res = new ApiPaging<OtherRoleDTO>();
            try
            {
                var link = from a in accountway.GetAll().Where(x => x.IsDel == false)
                           join ar in accountroleway.GetAll().Where(x => x.IsDel == false && x.RoleId == request.RoleId)  //获取供应商角色数据列表
                            on a.Id equals ar.AccountId
                           join r in roleway.GetAll().Where(x => x.IsDel == false) on ar.RoleId equals r.Id
                           select new OtherRoleDTO
                           {
                               AccountName = a.AccountName,
                               AccountPwd = "123",
                               Id = a.Id,
                               Addr = a.Addr,
                               Age = a.Age,
                               Info = a.Info,
                               Phone = a.Phone,
                               RealName = a.RealName,
                               RoleName = r.RoleName
                           };
                if (link.Any())
                {
                    log.LogInformation($"{DateTime.Now},成功获取供应商列表");
                    res.Totalcount = link.Count();
                    res.Code = StatusCode.Success;
                    res.Message = $"{DateTime.Now},成功获取供应商列表";
                    res.TotalPage = link.OrderBy(x => x.Id).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                }
                else
                {
                    res.Code = StatusCode.Fail;
                    res.Message = $"{DateTime.Now},获取供应商列表失败";
                }

                return Task.FromResult(res);

            }
            catch (Exception ex)
            {
                log.LogError($"{DateTime.Now}获取供应商列表时发生异常{ex.Message}");
                throw;
            }
        }
    }
}
